package com.gxa.learning.pc.controller;

import com.github.pagehelper.PageInfo;
import com.gxa.learning.core.common.ExceptionCode;
import com.gxa.learning.core.common.JsonResult;
import com.gxa.learning.core.exception.BusinessException;
import com.gxa.learning.core.pojo.Feedback;
import com.gxa.learning.core.pojo.Project;
import com.gxa.learning.core.util.StringUtil;
import com.gxa.learning.pc.service.FeedbackService;
import com.gxa.learning.pc.service.ProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version E-learning
 * @Author lonny
 * @Date 2021/12/2 18:58
 */
@RestController
@RequestMapping("/feedback")
@Api(value = "培训评估")
public class FeedbackController {


    @Autowired
    private FeedbackService feedbackService;

    @Autowired
    private ProjectService projectService;


    @PostMapping("/project")
    @ApiOperation(value = "反馈页首页项目列表")
    @ApiImplicitParam(name = "map", dataType = "map", paramType = "body", required = true)
    public Map<String, Object> feedbackAll(@RequestBody Map<String, Object> map) throws Exception {
        int page;
        int size;
        String pageNull = "page";
        String sizeNull = "size";
//        当前页码和数量为null的时候  设定默认值 页码1,页数10;
        if (map.get(pageNull) == null || StringUtil.isBlankOrNull(map.get(pageNull).toString())) {
            page = 0;
        } else {
            page = Integer.parseInt(map.get(pageNull).toString());
        }
        if (map.get(sizeNull) == null || StringUtil.isBlankOrNull(map.get(sizeNull).toString())) {
            size = 10;
        } else {
            size = Integer.parseInt(map.get(sizeNull).toString());
        }
        System.out.println(size);
        System.out.println(page);
//        判断查询的条件是否为空,为空的直接返回空
        String projectName = map.get("projectName") == null ? null : map.get("projectName").toString();
        String projectStatus = map.get("projectStatus") == null ? null : map.get("projectStatus").toString();
        String projectMan = map.get("projectMan") == null ? null : map.get("projectMan").toString();
        PageInfo<Map<String, Object>> projectPageInfo = projectService.getProjectList(page, size, projectName, projectStatus, projectMan);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.put("project", projectPageInfo.getList());
        jsonResult.put("total", projectPageInfo.getTotal());
        return jsonResult.getDataMap();
    }

    @GetMapping("/details")
    @ApiOperation(value = "查询项目详情,顶部")
    @ApiImplicitParam(name = "id", value = "培训项目id", dataType = "string", required = true)
    public Map<String, Object> byDetails(String id) throws Exception {
        Map<String, Object> byDetails = feedbackService.findByDetails(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(byDetails);
        return jsonResult.getDataMap();
    }

    @GetMapping("/student")
    @ApiOperation(value = "培训反馈学员列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "培训项目id", dataType = "string", required = true),
            @ApiImplicitParam(name = "userName", value = "被查询的培训人员姓名", dataType = "string"),
            @ApiImplicitParam(name = "company", value = "被查询的培训人员公司", dataType = "string"),
            @ApiImplicitParam(name = "position", value = "被查询的培训人员职位", dataType = "string")
    })
    public Map<String, Object> feedbackTraining(Integer page, Integer size, String id, String userName, String company, String position) throws Exception {

        PageInfo<Map<String, Object>> mapPageInfo = feedbackService.selectStudentList(page, size, id, userName, company, position);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.put("List", mapPageInfo.getList());
        jsonResult.put("total", mapPageInfo.getTotal());
        return jsonResult.getDataMap();
    }

    @GetMapping("/costList")
    @ApiOperation(value = "成本表")
    @ApiImplicitParam(name = "id", value = "培训项目id", dataType = "string", required = true)
    public Map<String, Object> feedbackCost(Integer page, Integer size, String id) throws Exception {
        if (page == null) {
            page = 0;
        }
        if (size == null) {
            page = 10;
        }
        PageInfo<Map<String, Object>> mapPageInfo = feedbackService.costList(page, size, id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.put("List", mapPageInfo.getList());
        jsonResult.put("total", mapPageInfo.getTotal());
        return jsonResult.getDataMap();
    }

    @GetMapping("/ratio")
    @ApiOperation(value = "反馈率")
    @ApiImplicitParam(name = "id", value = "培训项目id", dataType = "string", required = true)
    public Map<String, Object> feedbackTop(String id) throws Exception {
        Map<String, Object> feedbackRatio = feedbackService.findFeedbackRatio(id);
        JsonResult jsonResult = new JsonResult();
        Map<String, Object> map = new HashMap<>(3);
        map.put("feedbackPeople", feedbackRatio.get("user"));
        map.put("projectPeople", feedbackRatio.get("total"));
        map.put("totalPeople", feedbackRatio.get("ratio"));
        jsonResult.setData(map);
        return jsonResult.getDataMap();
    }

    @PostMapping("/proUpdate")
    @ApiOperation(value = "培训项目修改")
    public Map<String, Object> updatePro(@RequestBody Project project) throws BusinessException {
        Long projectId = project.getProjectId();
        if (projectId == null) {
            throw new BusinessException(ExceptionCode.ARGUMENTS_ERROR);
        }
        Integer integer = projectService.updateById(project);
        if (integer != 1) {
            throw new BusinessException(ExceptionCode.FAILED_OPTION);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("修改成功");
        return jsonResult.getDataMap();
    }

    @PostMapping("/insertFeedback")
    @ApiOperation("提交反馈")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "feedback", value = "feedback的Json对象", dataType = "json")
    })
    public Map insertFeedback(@RequestBody Feedback feedback) {
        JsonResult jsonResult = new JsonResult();
        feedback.setState(1);
        Integer integer = feedbackService.updateById(feedback);
        jsonResult.setCode("1");
        jsonResult.setMsg("提交成功");
        return jsonResult.getDataMap();
    }

    @GetMapping("/getFeedBack")
    @ApiOperation("获取反馈")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户的id", dataType = "long", required = true)
    })
    public Map getFeedBack(Long id) {
        JsonResult jsonResult = new JsonResult();
        //反馈的培训
        List<Map> feedBack = feedbackService.getFeedBack(id);
        //反馈的课程
        List<Map> courseFeedBack = feedbackService.getCourseFeedBack(id);
        jsonResult.setCode("1");
        jsonResult.setMsg("查询成功");
        jsonResult.put("count", feedBack.size() + courseFeedBack.size());
        List list = new ArrayList();
        for (Map map : feedBack) {
            list.add(map);
        }
        for (Map map : courseFeedBack) {
            list.add(map);
        }

        jsonResult.put("data", list);
        return jsonResult.getDataMap();
    }

    @GetMapping("/remind")
    @ApiOperation("提醒用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户的id", required = true),
            @ApiImplicitParam(name = "projectId", value = "项目的id", required = true)

    })
    public Map<String, Object> remind(String userId, String projectId) throws Exception {
        feedbackService.remindFeedBack(userId, projectId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("提醒成功");
        return jsonResult.getDataMap();
    }

}
